<?php
namespace app\common;
use think\Model;
use think\model\concern\SoftDelete;
class BaseModel extends Model{
	protected $paginate = 1;//默认分页 
	protected $updateTime = false;
	public static $validate;
	use SoftDelete; 
    protected $deleteTime = 'delete_time';
	protected static function init(){

		self::beforeInsert(function($model){
			$validate = str_replace('model', 'validate', get_class());
			self::$validate = new $validate; 
			return self::checkInsert($model);
		});
		self::beforeUpdate(function($model){

        	$validate = str_replace('model', 'validate', get_class());
			self::$validate = new $validate; 
			//更新内容改变更新时间
			self::afterWrite(function($model){
				$data = $model->getData();
				$data['update_time'] = time();
				$model->data($data,true)->save();
				
			});
             return self::checkUpdate($model);	
			
		});

		//更改model的save

	}
	public function getUpdateTimeAttr($value,$data)
    {
       if(!empty($value))
        return date("Y-m-d H:i:s",$value);
    }
	// public static function getaa(){
	// 	return new self;
	// }
	protected static  function checkInsert($model){
		
        return self::$validate->batch()->check($model->getData());
	}
	protected static  function checkUpdate($model){
        $rule = [];

        foreach ($model->getData() as $d_key => $d_value) {
        	
        	foreach (self::$validate->rule as $key => $value) {
        		$t_key = explode('|', $key)[0];
        		if($d_key == $t_key){
					$rule[$key] = $value;

        		}
        	}	
        }

        return self::$validate->batch()->check($model->getData(),$rule);
	}
}
?>